package com.mrslu.signkk.pojo;

import com.alibaba.fastjson2.JSON;
import com.aliyun.core.utils.StringUtils;
import com.mrslu.signkk.exception.ServiceException;
import com.mrslu.signkk.pojo.identity.UserRoleEnum;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.util.Date;


/**
 * @author 卢斌
 */
@Getter
@Setter
@NoArgsConstructor
public class User implements Serializable {

    private Integer id;

    private Integer orgId;

    private UserRoleEnum userRole;

    private String phoneNumber;

    private String userName;

    private Date createTime;

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }


    public static void checkUserParam(User user) {
        if (user == null) {
            throw new ServiceException("无效的用户参数");
        }
        if (StringUtils.isBlank(user.getUserName()) || user.getUserName().length() > 40) {
            throw new ServiceException("请填写有效的姓名");
        }
        if (StringUtils.isBlank(user.getPhoneNumber()) || user.getPhoneNumber().length() != 11 || !user.getPhoneNumber().equals(user.getPhoneNumber().trim())) {
            throw new ServiceException("请填写有效的手机号");
        }
        try {
            Long.parseLong(user.getPhoneNumber());
        } catch (Exception e) {
            throw new ServiceException("请填写11位有效手机号");
        }
        if (user.getPhoneNumber().charAt(0) != '1') {
            throw new ServiceException("请填写正确的手机号");
        }
    }

    
}